home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / DirtyInput / Analogue.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  6KB  |  180 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: DirtyInput                  Tulevagen 22       */
  8. /* File:    Analogue.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This is an Analogue Joystick reader. It is handy, easy and fast but */
  21. /* a bit naughty function since it reads directly from the hardware    */
  22. /* registers. It can read either port 1 ("the mouse port") or port 2   */
  23. /* ("the joystick port"). Note that an Analogue Joystick can have two  */
  24. /* buttons.                                                            */
  25. /*                                                                     */
  26. /* Synopsis: Analogue( port, x, y, button1, button2 );                 */
  27. /*                                                                     */
  28. /* port:     (UBYTE) Set the flag PORT1 if you want to check the first */
  29. /*           (mouse) port, or set the flag PORT2 if you want to check  */
  30. /*           the second (joystick) port.                               */
  31. /*                                                                     */
  32. /* x:        (UBYTE *) Horizontal value.                               */
  33. /*                                                                     */
  34. /* y:        (UBYTE *) Vertical value.                                 */
  35. /*                                                                     */
  36. /* button1:  (BOOL *) Button nr 1. TRUE=Pressed FALSE=Released.        */
  37. /*                                                                     */
  38. /* button2:  (BOOL *) Button nr 2. TRUE=Pressed FALSE=Released.        */
  39. /*                                                                     */
  40. /* ------------------------------------------------------------------- */
  41. /*                                                                     */
  42. /* HOW TO READ ANALOGUE JOYSTICKS:                                     */
  43. /* 1. Wait until the video beam have reached the top of the display.   */
  44. /*    Use for example the WaitTOF() function in the Graphics Library.  */
  45. /* 2. Activate the potentiometers. Two timing capacitors will slowly   */
  46. /*    be charged, and the charging speed depends on the position of    */
  47. /*    the stick.                                                       */
  48. /* 3. After a while the Amiga will check how much the capacitors have  */
  49. /*    been charged. The result is stored in the pot0dat/pot1dat field  */
  50. /*    in the Custom structure. (Bits 15-8 show the vertical position,  */
  51. /*    and bits 7-0 the horizontal.)                                    */
  52. /*    An analogue joystick can have two buttons, and their status is   */
  53. /*    stored in the joy0dat/joy1dat field in the Custom structure.     */
  54. /*    (If button 1 is pressed bit 1 is set, and if button 2 is pressed */
  55. /*    bit 9 is set.)                                                   */
  56. /* 4. Go back to point 1.                                              */
  57.  
  58.  
  59.  
  60. #include <exec/types.h>          /* UBYTE, BOOL etc       */
  61. #include <graphics/gfxbase.h>    /* struct GfxBase        */
  62. #include <graphics/gfxmacros.h>  /* CINIT(), CMOVE(), etc */
  63. #include <hardware/custom.h>     /* struct Custom         */
  64.  
  65.  
  66.  
  67. #define XMASK    0x00FF /* Horizontal: Bits 7 - 0 */
  68. #define YMASK    0xFF00 /* Vertical: Bits 15 - 8  */
  69.  
  70. #define ANALOGUE 0x0001 /* Activate the potentiometers. */
  71.  
  72. #define PORT1 1         /* "Mouse port"    */
  73. #define PORT2 2         /* "Joystick port" */
  74.  
  75.  
  76.  
  77. /* Pointer to the Graphics Library: */
  78. struct GfxBase *GfxBase;
  79.  
  80. /* Declare a Custom structure. This structure will */
  81. /* automatically be initialized! (Nice isn't it?)  */
  82. extern struct Custom far custom;
  83.  
  84.  
  85.  
  86. /* Declare the functions: */
  87. void main();
  88. void Analogue(
  89.   UBYTE port,
  90.   UBYTE *x,
  91.   UBYTE *y,
  92.   BOOL *button1,
  93.   BOOL *button2
  94. );
  95.  
  96.  
  97.  
  98. void main()
  99. {
  100.   int timer = 0;     /* Counter.                    */
  101.   UBYTE x, y;        /* Horizontal/Vertical values. */
  102.   BOOL fire1, fire2; /* Button 1 and Button 2.      */
  103.  
  104.  
  105.  
  106.   /* Open the Graphics library: */
  107.   GfxBase = (struct GfxBase *)
  108.     OpenLibrary( "graphics.library", 0 );
  109.   if( !GfxBase )
  110.     exit( -1 ); /* ERROR! Could NOT open the Graphics library! */
  111.  
  112.  
  113.  
  114.   printf( "  X   Y   Button1  Button2\n---------------------------\n" );
  115.   while( timer < 100 )
  116.   {
  117.     Analogue( PORT2, &x, &y, &fire1, &fire2 );
  118.     
  119.     timer++;
  120.  
  121.     printf( "[%3d %3d] %8s %8s\n", x, y,
  122.       fire1 ? "Pressed " : "Released",
  123.       fire2 ? "Pressed " : "Released" );
  124.   }
  125.  
  126.  
  127.  
  128.   /* Close the Graphics Library: */
  129.   CloseLibrary( GfxBase );
  130.  
  131.  
  132.  
  133.   /* Everything OK, time to leave: */
  134.   exit( 0 );
  135. }
  136.  
  137.  
  138.  
  139. void Analogue(
  140.   UBYTE port,
  141.   UBYTE *x,
  142.   UBYTE *y,
  143.   BOOL *button1,
  144.   BOOL *button2
  145. )
  146. {
  147.   UWORD pot; /* The potentiometer values. */
  148.   UWORD joy; /* Button 1 and 2.           */
  149.  
  150.  
  151.   if( port == 1 )
  152.   {
  153.     /* Port 1 */
  154.     pot = custom.pot0dat;
  155.     joy = custom.joy0dat;
  156.   }
  157.   else
  158.   {
  159.     /* Port 2 */
  160.     pot = custom.pot1dat;
  161.     joy = custom.joy1dat;
  162.   }
  163.   
  164.  
  165.   *x = pot & XMASK;        /* Horizontal: Bits 7 - 0 */
  166.   *y = (pot & YMASK) >> 8; /* Vertical: Bits 15 - 8  */
  167.  
  168.  
  169.   *button1 = joy & 0x0002 ? TRUE : 0; /* Left Button (1)  */
  170.   *button2 = joy & 0x0200 ? TRUE : 0; /* Right Button (2) */
  171.  
  172.  
  173.   /* Wait until the video beam has reached the top of the display: */
  174.   WaitTOF();
  175.  
  176.  
  177.   /* Activate the potentiometers: */
  178.   custom.potgo = ANALOGUE;
  179. }
  180.